package me.zhengjie.modules.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.Companies;
import me.zhengjie.modules.service.CompaniesService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api("公司信息api")
@RestController
@RequiredArgsConstructor
@RequestMapping("/com")
public class CompaniesController {

    private final CompaniesService companiesService;

    @GetMapping("/findAllByPage")
    public List<Companies> findAllByPage(Integer pageNo, Integer pageSize){
        return companiesService.findAllByPage((pageNo-1)*pageSize,pageSize);
    }

    /**
     * 解析excel文件  完成数据添加
     * @param file
     * @return
     * @throws Exception
     */
    @AnonymousAccess
    @GetMapping("/upload")
    public int upload(@RequestPart("file") MultipartFile file) throws Exception{
        HSSFWorkbook workbook=new HSSFWorkbook(file.getInputStream());
        HSSFSheet sheet=workbook.getSheet("Export");
        List<Companies> list=new ArrayList<>();
        Companies companies=null;
        HSSFRow row=null;
        HSSFCell cell=null;
        for (int i=1;i<sheet.getLastRowNum()+1;i++){
            row=sheet.getRow(i);
            companies=new Companies();
            cell=row.getCell(0);
            companies.setSocialCreditCode(cell.getStringCellValue());
            cell=row.getCell(1);
            companies.setOrganizationCode(cell.getStringCellValue());
            cell=row.getCell(2);
            companies.setCompanyName(cell.getStringCellValue());
            cell=row.getCell(3);
            companies.setAdministrativeDivisionCode(cell.getStringCellValue());
            cell=row.getCell(4);
            companies.setCountyDistrict(cell.getStringCellValue());
            cell=row.getCell(5);
            companies.setAddress(cell.getStringCellValue());
            cell=row.getCell(6);
            companies.setMainBusinessActivities(cell.getStringCellValue());
            cell=row.getCell(7);
            companies.setIndustryCode(cell.getStringCellValue());
            cell=row.getCell(8);
            companies.setBureauStatisticsCode(cell.getStringCellValue());
            companies.setCreateBy("3");
            companies.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            list.add(companies);
        }
        try{
            int num=companiesService.addCompaniesByList(list);
            if(num==sheet.getLastRowNum()+1){
                return num;
            }else{
                return num;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @ApiOperation("查询首页的基本信息")
    @AnonymousAccess
    @GetMapping("/queryAllCount")
    public List<Map<String, Object>> queryAllCount(Integer honor,Integer honor2){
        System.out.println(honor2+"===========================");
        //return companiesService.queryAllCount();
        return companiesService.queryAllCountByCondition(honor,honor2);
    }
}
